// pages/pancun/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    materialText:'',
    bar_code:'',
    materialId:'',
    id:'',
    mdId:'',
    num:'',
    // type:'',
    // warehouseText:'',
    // warehouseValue:[],
    // warehouseId:'',
    wText:'',
    wValue:[],
    wId:'',
    wareHouseData:[],
    backNum:'',
    isSub:2,
    wlInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getWareHouse();
    this.scanCode();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // getWareHouse(){
  //   let that=this;

  //   console.log(111111)
  //   app.common.http_post('/miniProgram/Other/getWarehouse',{},function(res){
  //         let data=res.data;
  //        that.setData({wareHouseData:data});
  //       })
  // }, 
  warehousePicker(){
    this.setData({ warehouseVisible: true});
  },
  wPicker(){
    this.setData({ wVisible: true});   
  },
  selectWareHouse(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
  },
  onPickerCancel(e){
    const { key } = e.currentTarget.dataset.key;
  
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  getWareHouse(){
    let that=this;
    console.log(111111)
    app.common.http_post('/miniProgram/Other/getWarehouse',{},function(res){
          let data=res.data;
         that.setData({wareHouseData:data});
      
        })
  }, 
  burD(e){
    this.setData({bar_code:e.detail.value,materialId:'',materialText:''})
    this.getInfo();
  },
  selectMaterial(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
    this.getInfo();
  },
  scanCode(){
    let that=this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
    console.log('scr',res)
        if (res.errMsg === "scanCode:ok") {
          let code=res.result;
        that.setData({bar_code:code,materialId:'',materialText:''})
        that.getwlInfo();
        // that.getInfo()
        }
      }
    })
  },
  getwlInfo(){
    let that =this;
    let bar_code=that.data.bar_code;
    if(bar_code==''){
      app.common.alert('请选择物料');
      return;
    }
    
    let param={bar_code:bar_code};
    app.common.http_post('/miniProgram/Other/getMaterialInfo',param,function(res){
  console.log('res',res)
      let data=res.data;
      console.log('wlData',data);
      let text=data.wName;
      let val=data.warehouse_id;
      that.setData({wId:val,wText:text,wValue:[val]})
  that.getInfo();
    })
  },
  getInfo(){
    let that=this;
    that.setData({detail:{}})
    let bar_code=that.data.bar_code;
    let materialId=that.data.materialId;
    let wId=that.data.wId;
    if(wId==''){
      app.common.alert('请选择仓库');
      return;
    }
    if(bar_code==''&&materialId==''){
      return;
    }
    
    let param={bar_code:bar_code,mdId:materialId,wId:wId};
    app.common.http_post('/miniProgram/Other/getKucunInfo',param,function(res){
  console.log('res',res)
      let data=res.data;
      console.log('wlData',data);
      let kcNum=data.countNum-data.sendNum;

     that.setData({detail:data,mdId:data.mdId,backNum:kcNum});
    })
  },
  bindKeyInput: function (e) {
    this.setData({materialText:e.detail.value,bar_code:''})
    let that=this;
    let param={mdName:e.detail.value};
    console.log('ss',e.detail.value)
    if(e.detail.value==''){
      this.setData({mdId:'',materialVisible:false})
      return;
    }
    app.common.http_post('/miniProgram/Other/getMaterial',param,function(res){
      let data=res.data;
      console.log('wlData',data);
      that.setData({materialData:data})
      if(data.length>0&&e.detail.value!=''){
        that.setData({materialVisible:true})
      }
        
     
    })
    

  },
  setNum(e){
    this.setData({num:e.detail.value})
  },
  onSure(){
    console.log('ddd',this.data)
    let that=this;
    let mdId=that.data.mdId;
    let wId=that.data.wId;
    let backNum=that.data.backNum;
    let num=that.data.num;
    let param={
      mdId:mdId,
      wId:wId,
      backNum:backNum,
      num:num
    };
    console.log('param',param)
    if(mdId==''||wId==''||num==''||num<0){
      app.common.alert('参数填写有误');
      return false;
    }
  if(that.data.isSub==1){
    return
  }
  app.common.showLoading();
  that.setData({isSub:1})
    app.common.http_post('/miniProgram/InventoryOrder/onSure',param,function(res){  
      console.log('res',res) 
      that.setData({isSub:2})
      app.common.hideLoading();
      app.common.alert(res.msg)
      setTimeout(function(){
        app.common.go_back();
      },500)
     })
  }
})